package com.hckjsec.common.utils.uuid;

import com.alibaba.fastjson.JSON;
import com.baidu.aip.ocr.AipOcr;
import com.hckjsec.common.config.BdIdCardResult;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Hashtable;

public class BdOrcUtil {

    /**
     * 功能描述: 百度OCR
     */
    public static BdIdCardResult getIdCardInfo(String side,String img, String appid, String appkey, String appsecret) {
        BdIdCardResult resultView = null;
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(appid, appkey, appsecret);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        // 强制设置参数
        options.put("detect_direction", "true");
        options.put("detect_risk", "true");
        //options.put("detect_photo", "true");

        // 参数为本地图片路径
        JSONObject idCardInfoHT = client.idcard(img, side, options);
        try {
            // 返回状态
            String status = String.valueOf(idCardInfoHT.get("image_status"));
            // 拍的证件类型 是否是正常的身份证
            String type = String.valueOf(idCardInfoHT.get("risk_type"));

            // 图片没域被经过软件修改过
            if(idCardInfoHT.isNull("edit_tool")) {
                resultView = new BdIdCardResult(status,type);
                if(side.equals("back")) {
                    resultView.setBackInfo(idCardInfoHT.getJSONObject("words_result"));
                }else {
                    resultView.setFrontInfo(idCardInfoHT.getJSONObject("words_result"));
                }
            }else {
                resultView = new BdIdCardResult(idCardInfoHT.get("edit_tool").toString());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultView;
    }
}
